package com.han.fu.api.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.han.fu.api.business.entity.Channel;
import com.han.fu.api.business.mapper.ChannelMapper;
import com.han.fu.api.business.model.ao.*;
import com.han.fu.api.business.model.vo.ChannelInfoVo;
import com.han.fu.api.business.model.vo.ChannelVo;
import com.han.fu.api.business.service.IChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 渠道接口实现类
 *
 * @author Star.zhu
 * @since 2020-03-26
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements IChannelService {

    /**
     * 渠道分页查询
     *
     * @param ao 查询条件
     * @return Page<ChannelVo> 分页数据
     */
    @Override
    public Page<ChannelVo> queryChannelPage(ChannelPageAo ao) {
        return baseMapper.queryChannelPage(ao);
    }

    /**
     * 渠道明细查询
     *
     * @param id 明细ID
     * @return
     */
    @Override
    public ChannelInfoVo queryChannelDetail(String id) {
        ChannelInfoVo channelInfoVo = new ChannelInfoVo();
        Channel channel = super.getById(id);
        if (null != channel) {
            BeanUtils.copyProperties(channel, channelInfoVo);
        }
        return channelInfoVo;
    }

    /**
     * 渠道新增数据
     *
     * @param ao 数据
     */
    @Override
    public void addChannel(ChannelAddAo ao) {
        Channel entity = new Channel();
        BeanUtils.copyProperties(ao, entity);
        super.save(entity);
    }

    /**
     * 渠道修改数据
     *
     * @param ao 数据
     */
    @Override
    public void updateChannel(ChannelUpdateAo ao) {
        Channel entity = new Channel();
        BeanUtils.copyProperties(ao, entity);
        super.updateById(entity);
    }

    /**
     * 渠道删除数据
     *
     * @param ao 数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteChannel(ChannelDeleteAo ao) {
        for (String id : ao.getIds()) {
            super.removeById(id);
        }
    }

    /**
     * 根据AppId和密钥查询渠道是否存在
     *
     * @param appId     AppId
     * @param secretKey 密钥
     * @return boolean
     */
    @Override
    public boolean queryChannelExist(String appId, String secretKey) {
        return SqlHelper.retBool(baseMapper.queryChannelCount(appId, secretKey));
    }

}